package huoye.shops.huoye.operation.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import huoye.shops.R;
import huoye.shops.base.BaseFragment;
import huoye.shops.huoye.mine.model.PowerList;
import huoye.shops.huoye.operation.activity.BangBoBlackCardActivity;
import huoye.shops.huoye.operation.activity.DiscountCancelActivity;
import huoye.shops.huoye.operation.activity.ManageEventActivity;
import huoye.shops.huoye.operation.activity.ManagePrizeActivity;
import huoye.shops.huoye.operation.activity.ManageProductActivity;
import huoye.shops.huoye.operation.activity.ManageSellActivity;
import huoye.shops.huoye.operation.activity.StatisticsActivity;
import huoye.shops.huoye.operation.presenter.OperationPresenter;
import huoye.shops.huoye.operation.view.IOperationView;

import static huoye.shops.huoye.operation.activity.ManagePrizeActivity.EXTRAS_INTENT_PRIZE_TYPE_URL;
import static huoye.shops.huoye.operation.activity.ManagePrizeActivity.EXTRAS_KEY_PRIZE_TYPE;


/**
 * A simple {@link Fragment} subclass.
 */
public class OperationFragment extends BaseFragment implements AdapterView.OnItemClickListener,IOperationView{


    //定义以及初始化数据
    private List<Map<String,Object>> dataList;
   // private int[] icon={R.mipmap.ic_operation_one,R.mipmap.ic_operation_two,R.mipmap.ic_operation_three,R.mipmap.ic_operation_four,R.mipmap.ic_operation_five,R.mipmap.ic_operation_six,R.mipmap.ic_operation_seven,R.mipmap.ic_operation_six};
   // Map<String, String> iconName = new HashMap<>();
     List<String> iconName = new ArrayList<>();
     List<Integer> icon = new ArrayList<>();

    private OperationPresenter mOperationPresenter;

    @BindView(R.id.operation_gv)
    GridView gridView;
    @Override
    public int getContentViewId() {
        return R.layout.fragment_operation;
    }

    @Override
    protected void initView(View view, @Nullable Bundle bundle) {

        mOperationPresenter = new OperationPresenter();
        mOperationPresenter.addTaskListener(this);
        mOperationPresenter.onStart();
        mOperationPresenter.getUserPowers();

    }

/*
    @Override
    public void onResume() {
        super.onResume();
        if(mOperationPresenter != null){
            mOperationPresenter.getUserPowers();
        }
    }*/

    private List<Map<String, Object>> getData() {
        for (int i=0; i<icon.size(); i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("img", icon.get(i));
            map.put("txt", iconName.get(i));
            dataList.add(map);
        }


        return dataList;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int j, long l) {
       // makeToast("我是"+iconName.get(i));

        switch (iconName.get(j)){
            case "销售管理":
                startActivity(ManageSellActivity.class);
                break;
            case "商品管理":
                startActivity(ManageProductActivity.class);
                break;
            case "活动管理":
                startActivityData(getActivity(),ManageEventActivity.class,isPowerBuildEvent);
                break;
            case "奖品管理":
                Intent intent = new Intent(getActivity(), ManagePrizeActivity.class);
                intent.putExtra(EXTRAS_KEY_PRIZE_TYPE,"2");
                startActivity(intent);
                break;
            case "帮播黑卡":
                startActivity(BangBoBlackCardActivity.class);
                break;
            case "统计信息":
                startActivity(StatisticsActivity.class);
                break;
            case "折扣核销":
                startActivity(DiscountCancelActivity.class);
                break;
        }



    }

    private boolean isPowerBuildEvent = false;


     List<PowerList> mList = new ArrayList<>();
    @Override
    public void setPowerList(List<PowerList> list) {
        this.mList = list;

        for (int i=0; i< list.size(); i++) {
            if(list.get(i).getPowerEng().equals("publish")){
                iconName.add("商品管理");
                icon.add(R.mipmap.ic_operation_two);
            }else if(list.get(i).getPowerEng().equals("product")){
                if(!iconName.get(0).equals("商品管理")){
                    iconName.add("商品管理");
                    icon.add(R.mipmap.ic_operation_two);
                }
                iconName.add("销售管理");
                icon.add(R.mipmap.ic_operation_one);
            }else if(list.get(i).getPowerEng().equals("onecard")){
                iconName.add("帮播黑卡");
                iconName.add("折扣核销");
                icon.add(R.mipmap.ic_operation_five);
                icon.add(R.mipmap.ic_operation_seven);
            }else if(list.get(i).getPowerEng().equals("activity")){
                iconName.add("活动管理");
                iconName.add("奖品管理");
                icon.add(R.mipmap.ic_operation_three);
                icon.add(R.mipmap.ic_operation_four);
            }else if(list.get(i).getPowerEng().equals("shopActivity")){
                isPowerBuildEvent = true;
            }else if(list.get(i).getPowerEng().equals("statistical")){
                iconName.add("统计信息");
                icon.add(R.mipmap.ic_operation_six);
            }

        }


        dataList = new ArrayList<>();
        SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), getData(), R.layout.item_operation_show, new String[]{"img", "txt"}, new int[]{R.id.item_operation_iv, R.id.item_operation_tv});
        gridView.setAdapter(simpleAdapter);
        gridView.setOnItemClickListener(this);


    }






}
